CREATE PROCEDURE [dbo].[asi_GenerateExpectedPaymentSet]
(@newExpectedPaymentSetKey uniqueidentifier,
@monthYear datetime,
@userKey uniqueidentifier,
@batchSize integer,
@doNotLoop bit,
@clearExistingValues bit,
@updateHeaderFlag bit
)
AS
BEGIN
DECLARE @expectedPaymentSetKey uniqueidentifier
DECLARE @foundExistingExpectedPaymentSet bit
DECLARE @monthDay integer
DECLARE @paymentMonth integer
DECLARE @paymentYear integer
DECLARE @monthYearEnd datetime
DECLARE @setStatusComplete smallint
DECLARE @setStatusGenerating smallint
DECLARE @setStatusCompleteWithError smallint
DECLARE @paymentStatusPending smallint
DECLARE @paymentStatusAwaiting smallint
DECLARE @paymentStatusProcessed smallint
DECLARE @errorMessage NVARCHAR(4000);
DECLARE @errorSeverity INT;
DECLARE @errorState INT;
SET NOCOUNT ON
SET @monthDay = DATEPART(DAY,@monthYear)
IF @monthDay<>1
BEGIN
RAISERROR ( N'MonthYear Date must have day set to 1',16,1);
RETURN
END
SET @paymentMonth = DATEPART(MONTH,@monthYear)
SET @paymentYear = DATEPART(YEAR,@monthYear)
SET @monthYearEnd = DATEADD(MONTH,1,@monthYear)
SET @monthYearEnd = DATEADD(DAY,-1,@monthYearEnd)
SET @setStatusComplete = 1
SET @setStatusGenerating = 2
SET @setStatusCompleteWithError = 3
SET @paymentStatusPending = 1
SET @paymentStatusAwaiting = 2
SET @paymentStatusProcessed = 3
IF @batchSize <=0
SET @batchSize = 50
IF EXISTS (SELECT 1 FROM RecurringDonationExpectedPaymentSet WHERE ExpectedPaymentDate=@monthYear)
BEGIN
SET @foundExistingExpectedPaymentSet = 1
SELECT @expectedPaymentSetKey=[RecurringDonationExpectedPaymentSetKey]
FROM [RecurringDonationExpectedPaymentSet]
WHERE [ExpectedPaymentDate]=@monthYear
IF @newExpectedPaymentSetKey <> @expectedPaymentSetKey
BEGIN
RAISERROR ( N'The ExpectedPaymentSetKey value passed into the Procedure does not match the existing ExpectedPaymentSetKey',16,1);
RETURN
END
END
ELSE
BEGIN
SET @foundExistingExpectedPaymentSet = 0
IF @newExpectedPaymentSetKey IS NOT NULL
SET @expectedPaymentSetKey = @newExpectedPaymentSetKey
ELSE
SET @expectedPaymentSetKey = NEWID()
END
IF @clearExistingValues = 1 AND @foundExistingExpectedPaymentSet = 1
BEGIN
BEGIN TRANSACTION
DELETE
FROM [RecurringDonationExpectedPayment]
WHERE [RecurringDonationExpectedPaymentSetKey] = @expectedPaymentSetKey
AND [RecurringDonationExpectedPaymentStatusCode] <> @paymentStatusProcessed
COMMIT TRANSACTION
END
DECLARE @recurringDonationTable TABLE (
[StartDate] datetime NOT NULL,
[EndDate] datetime NULL,
[RecurringDonationFrequencyCode] integer NOT NULL,
[RecurringDonationCommitmentKey] uniqueidentifier NOT NULL
)
;WITH RecurringDonationTable AS (
SELECT StartDate, EndDate, RecurringDonationFrequencyCode, RecurringDonationCommitmentKey
FROM RecurringDonationCommitment
WHERE CommitmentStatusInd='A' AND
StartDate<=@monthYearEnd AND
(
EndDate IS NULL
OR
EndDate>=@monthYear
)
UNION ALL
SELECT DATEADD(m, 12/RecurringDonationFrequencyCode, StartDate), EndDate, RecurringDonationFrequencyCode, RecurringDonationCommitmentKey
FROM RecurringDonationTable a
WHERE DATEADD(m, 12/RecurringDonationFrequencyCode, StartDate) <= ISNULL(EndDate,@monthYearEnd)
AND DATEADD(m, 12/RecurringDonationFrequencyCode, StartDate)<=@monthYearEnd
)
INSERT INTO @recurringDonationTable (StartDate, EndDate, RecurringDonationFrequencyCode, RecurringDonationCommitmentKey)
SELECT StartDate, EndDate, RecurringDonationFrequencyCode, RecurringDonationCommitmentKey
FROM RecurringDonationTable
WHERE StartDate<=@monthYearEnd AND StartDate>=@monthYear
OPTION (maxrecursion 0);
BEGIN TRANSACTION
BEGIN TRY
IF @foundExistingExpectedPaymentSet = 0
BEGIN
INSERT INTO [dbo].[RecurringDonationExpectedPaymentSet] ([RecurringDonationExpectedPaymentSetKey], [ExpectedPaymentDate],
[RecurringDonationExpectedPaymentSetStatusCode], [CreatedByUserKey],
[CreatedOn], [UpdatedByUserKey], [UpdatedOn])
VALUES (@expectedPaymentSetKey, @monthYear, @setStatusGenerating, @userKey, GETDATE(), @userKey, GETDATE())
END
ELSE
BEGIN
UPDATE [dbo].[RecurringDonationExpectedPaymentSet]
SET [UpdatedByUserKey] = @userKey, [UpdatedOn] = GETDATE(), [RecurringDonationExpectedPaymentSetStatusCode] = @setStatusGenerating
WHERE [RecurringDonationExpectedPaymentSetKey] = @expectedPaymentSetKey
END
IF @@TRANCOUNT > 0
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
SELECT
@errorMessage = ERROR_MESSAGE(),
@errorSeverity = ERROR_SEVERITY(),
@errorState = ERROR_STATE();
RAISERROR (@errorMessage,
@errorSeverity,
@errorState
);
END CATCH
BEGIN TRANSACTION
BEGIN TRY
DECLARE @isDone bit
SET @isDone = 0
WHILE (@isDone = 0)
BEGIN
INSERT INTO [dbo].[RecurringDonationExpectedPayment]([RecurringDonationExpectedPaymentKey], [RecurringDonationExpectedPaymentSetKey],
[RecurringDonationCommitmentKey], [OpportunityKey], [DonationAmount], [PaymentMethod],
[Distribution], [Appeal], [Campaign], [Fund], [Narrative], [MatchReference], [BranchSortCode],
[BankAccountNumber], [BankAccountName], [ResultingTransNum], [ResultingTransLineNum],
[RecurringDonationExpectedPaymentStatusCode], [CreatedByUserKey], [CreatedOn], [UpdatedByUserKey], [UpdatedOn])
SELECT TOP (@batchSize) NEWID(), @expectedPaymentSetKey, c.RecurringDonationCommitmentKey, NULL, c.DonationAmount, c.PaymentMethod,
c.Distribution, c.Appeal, c.Campaign, c.Fund, N'', c.MatchReference, c.BranchSortCode, c.BankAccountNumber,
c.BankAccountName, 0, 0, @paymentStatusAwaiting, @userKey, GETDATE(), @userKey, GETDATE()
FROM RecurringDonationCommitment c
INNER JOIN @recurringDonationTable r ON c.RecurringDonationCommitmentKey = r.RecurringDonationCommitmentKey
LEFT OUTER JOIN RecurringDonationExpectedPayment e ON c.RecurringDonationCommitmentKey=e.RecurringDonationCommitmentKey
AND e.[RecurringDonationExpectedPaymentSetKey] = @expectedPaymentSetKey
WHERE e.RecurringDonationCommitmentKey IS NULL
IF @@ROWCOUNT = 0
SET @isDone = 1
IF @@TRANCOUNT > 0
COMMIT TRANSACTION
IF @doNotLoop = 1
BREAK
END
IF @isDone = 1 AND @updateHeaderFlag=1
BEGIN
BEGIN TRANSACTION
UPDATE [dbo].[RecurringDonationExpectedPaymentSet]
SET [UpdatedByUserKey] = @userKey, [UpdatedOn] = GETDATE(),
[RecurringDonationExpectedPaymentSetStatusCode] = @setStatusComplete
WHERE [RecurringDonationExpectedPaymentSetKey] = @expectedPaymentSetKey
IF @@TRANCOUNT > 0
COMMIT TRANSACTION
END
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
SELECT
@errorMessage = ERROR_MESSAGE(),
@errorSeverity = ERROR_SEVERITY(),
@errorState = ERROR_STATE();
IF @updateHeaderFlag = 1
BEGIN
BEGIN TRANSACTION
UPDATE [dbo].[RecurringDonationExpectedPaymentSet]
SET [UpdatedByUserKey] = @userKey, [UpdatedOn] = GETDATE(),
[RecurringDonationExpectedPaymentSetStatusCode] = @setStatusCompleteWithError
WHERE [RecurringDonationExpectedPaymentSetKey] = @expectedPaymentSetKey
IF @@TRANCOUNT > 0
COMMIT TRANSACTION
END
RAISERROR (@errorMessage,
@errorSeverity,
@errorState
);
END CATCH
END
GO
GRANT EXECUTE ON [dbo].[asi_GenerateExpectedPaymentSet] TO [IMIS]
GO